<?php
/**
 * shopify独立站产品
 * (model)
 */
namespace App\Models\Product;

use App\Models\Basics\ShopifyAccount;
use App\Models\Basics\SysCountry;
use App\Models\Basics\SysPlat;
use App\Util\User;
use Illuminate\Database\Eloquent\Model;

class ProductShopifySku extends Model{

    protected $table = 'product_shopify_sku';

    protected $guarded = [];

    public static $_barcodeType = [
        1 => 'UPC码',
        2 => 'EAN码',
        3 => '无',
    ];

    public static $_status = [
        1 => '启用',
        2 => '停用',
    ];

    public static function displayStatus() {
        return ['1' => '<i class="fa fa-check text-green"></i>', '2' => '<i class="fa fa-close text-red"></i>'];
    }

    public function sku(){
        return $this->belongsTo(ProductSku::class,'psku_id');
    }

    public function account(){
        return $this->belongsTo(ShopifyAccount::class,'account_id');
    }

    public function country(){
        return $this->belongsTo(SysCountry::class,'country_id');
    }

    /**
     *  关联平台
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function sys_plat(){
        return $this->belongsTo(SysPlat::class, 'plat_id', 'plat_id');
    }

    public static function getValueByCode($code, $field){
        return self::query()->where('seller_sku', $code)->value($field);
    }

    public function skuClassify(){
        return $this->hasOneThrough(
            ProductClassify::class,
            ProductSku::class,
            'id',//中间表
            'id',//目标表
            'psku_id',//当前模型
            'classify_id'//中间表
        );
    }

    public static function getDownloadData($res)
    {
        foreach ($res as $model){
            $temp = [
                $model->seller_sku ?? '',
                $model->sku->psku_code ?? '',
                $model->sku->psku_cn,
                ProductClassify::$_season[$model->skuClassify->season ?? 3],
                $model->skuClassify->title ?? '',
                $model->account->account ?? '',
                $model->country->country ?? '',
                User::getUsernameById($model->seller_id),
                self::$_barcodeType[$model->barcode_type],
                $model->barcode,
                User::getUsernameById($model->cuser_id),
                $model->created_at,
                self::$_status[$model->status],
            ];
            yield $temp;
        }
    }

}
